package com.si.ui;

import java.awt.Component;
import java.awt.TextField;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.LayoutStyle;

import com.si.ui.enumn.EControlPanel;
import com.si.ui.enumn.ECollageAlgorithm;

class ControlPanel extends JPanel implements IWidget {

	private static final long serialVersionUID = -6478898693992338325L;

	private TextField txtSearch;
	private JButton btnSearch;
	private JButton btnHiRes;
	private JComboBox comboAlgo;
	private JProgressBar bar;

	@Override
	public void initialize(final Map<String, Component> components) {
		this.buildComponents(components);
		this.buildLayout();
	}

	private void buildComponents(final Map<String, Component> components) {
		this.txtSearch = new TextField();
		components.put(EControlPanel.TextField_Search.toString(), this.txtSearch);
		this.btnSearch = new JButton("Search");
		this.btnSearch.setActionCommand(EControlPanel.Button_Search.toString());
		components.put(EControlPanel.Button_Search.toString(), this.btnSearch);
		this.btnHiRes = new JButton("Hi-Res");
		this.btnHiRes.setActionCommand(EControlPanel.Button_HiRes.toString());
		this.btnHiRes.setEnabled(false);
		components.put(EControlPanel.Button_HiRes.toString(), this.btnHiRes);
		this.comboAlgo = new JComboBox();
		this.comboAlgo.setModel(new DefaultComboBoxModel(ECollageAlgorithm.values()));
		components.put(EControlPanel.Combo_Algorithm.toString(), this.comboAlgo);
		this.bar = new JProgressBar();
		this.bar.setStringPainted(true);
		components.put(EControlPanel.ProgressBar.toString(), this.bar);
	}

	private void buildLayout() {
		this.setBorder(BorderFactory.createTitledBorder("Control"));
		final GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(this.bar, 690, 690, 690)
								.addGroup(layout.createSequentialGroup()
										.addComponent(this.txtSearch, GroupLayout.PREFERRED_SIZE, 350, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(this.btnSearch)
										.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(this.btnHiRes)
										.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(this.comboAlgo, 0, 165, Short.MAX_VALUE)
										.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		))));
		layout.setVerticalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(this.btnSearch)
										.addComponent(this.btnHiRes)
										.addComponent(this.comboAlgo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addComponent(this.txtSearch, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
						                .addComponent(this.bar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
		));
	}
}
